package com.ruoyi.canteen.controller.v1;

import com.ruoyi.canteen.domain.CanteenMdDeliver;
import com.ruoyi.canteen.service.ICanteenMdDeliverService;
import com.ruoyi.canteen.service.ICanteenMdService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 门店配送表
 * @Author Ryan
 * @Date 2021/9/29 11:44
 * @Version 1.0
 */
@RestController
@RequestMapping("/v1/mdDeliver")
public class MdDeliverApiController extends BaseController {

    @Autowired
    private ICanteenMdDeliverService mdDeliverService;

    /**
     * 获取门店配送列表信息
     */
    @GetMapping(value = "/getList/{mdId}")
    public AjaxResult getDetail(@PathVariable("mdId") Long mdId) throws ParseException {
        CanteenMdDeliver mdDeliver = new CanteenMdDeliver();
        mdDeliver.setMdId(mdId);
        return AjaxResult.success(selectCurDateDeliverList(mdId));
    }

    private List<CanteenMdDeliver> selectCurDateDeliverList(Long mdId) throws ParseException {
        // 未在规定时间内下单,无效单
        Integer timeType = 0;
        ArrayList<CanteenMdDeliver> returnList = new ArrayList<CanteenMdDeliver>();
        CanteenMdDeliver queryMdDeliver = new CanteenMdDeliver();
        queryMdDeliver.setMdId(mdId);
        List<CanteenMdDeliver> dbList = mdDeliverService.selectCurDeliverListByMdId(mdId);
        int mdDeliverCount = dbList.size();
        if(mdDeliverCount > 0){
            String format = "HH:mm:ss";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            String timeStr = simpleDateFormat.format(new Date());
            Date timeDate = simpleDateFormat.parse(timeStr);

            for(int i=0; i<mdDeliverCount; i++){
                CanteenMdDeliver canteenMdDeliver = dbList.get(i);
                if(StringUtils.isNotEmpty(canteenMdDeliver.getOrderBegTime())
                        && StringUtils.isNotEmpty(canteenMdDeliver.getOrderEndTime())){
                    Date startTime = simpleDateFormat.parse(canteenMdDeliver.getOrderBegTime());
                    Date endTime = simpleDateFormat.parse(canteenMdDeliver.getOrderEndTime());
                    if (timeDate.after(startTime) && timeDate.before(endTime)) {
                        returnList.add(canteenMdDeliver);
                    }
                }
            }
        }
        return returnList;
    }

}
